<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
</head>
<body>
    <button id="btn">发送验证码</button>
    <script>
        var btn = document.querySelector("#btn");
        var num = 5;
        // btn.onclick = function(){
        //   var timer = setInterval(function(){
           
        //     if(num>0){ 
        //         btn.innerHTML = num;
        //         btn.disabled = true;
        //         num--
        //     }
        //     else{
        //         clearInterval(timer)
        //         btn.disabled = false;
        //         btn.innerHTML = "发送验证码"
        //         num = 5;
        //     }
        //   },1000)
        // }
        btn.onclick = function go(){
            setTimeout(function(){
                if(num>0){ 
                btn.innerHTML = num;
                btn.disabled = true;
                num--
                go()
            }
            else{
                btn.disabled = false;
                btn.innerHTML = "发送验证码"
                num = 5;
            }
            },1000)
        }
    </script>
</body>
</html>